package net.xdclass.video.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.xdclass.video.common.Result;
import net.xdclass.video.crawler.utils.StringUtils;
import net.xdclass.video.entity.Details;
import net.xdclass.video.entity.FileOne;
import net.xdclass.video.entity.Images;
import net.xdclass.video.mapper.DetailsMapper;
import net.xdclass.video.mapper.FileMapper;
import net.xdclass.video.mapper.ImagesMapper;
import net.xdclass.video.service.DetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static java.lang.System.getProperty;

@RestController
public class DetailsController {
    @Autowired
    private DetailsMapper detailsMapper;
    @Autowired
    private DetailsService detailsService;
    @Autowired
    private ImagesMapper imagesMapper;
    @Autowired
    private FileMapper fileMapper;
   private String CoverList;

    private String MaterialList;
    private static final String FILE_UPLOAD_PATH = getProperty("user.dir")
            + File.separator
            + "src" + File.separator
            + "main" + File.separator
            + "resources"
            + File.separator
            + "static/files" + File.separator
            + "image" + File.separator;
    @Autowired

    private RedisTemplate redisTemplate;

    @PostMapping("/api/details/save")
    public Result save(@RequestBody Details details){
        detailsService.saveOrUpdate(details);
        return Result.success();
    }


    //上传图片
    @PostMapping("/api/details/description")
    public Result  details(@RequestParam("cover") MultipartFile cover ,String name, String classify, String description, String actors) throws IOException {
        String originalFilename = cover.getOriginalFilename();
        String type = FileUtil.extName(originalFilename);

        long size=cover.getSize();
        String UUID= IdUtil.fastSimpleUUID()+ StrUtil.DOT+type;
        File uploadFile = new File(FILE_UPLOAD_PATH+UUID);
        String md5 = SecureUtil.md5(cover.getInputStream());
        Details dbDetails = getDetailsByMd5(md5);
        String url;
        if (dbDetails !=null){
            url = dbDetails.getCover();
            boolean exist=FileUtil.exist(FILE_UPLOAD_PATH+url.substring(url.lastIndexOf("/")+1));
            if (!exist){

                cover.transferTo(uploadFile);
                url="http://localhost:9090/files/image/"+UUID;
            }
        }else {
            cover.transferTo(uploadFile);
             url="http://localhost:9090/files/image/"+UUID;
        }


        Images images=new Images();
        images.setName(name);
        images.setSize(size);
        images.setOriginalFilename(originalFilename);
        images.setType(type);
        images.setMd5(md5);
        images.setCover(url);
        imagesMapper.insert(images);

        Details details=new Details();
        details.setDescription(description);
        details.setCover(url);
        details.setClassify(classify);
        details.setActors(actors);
        details.setName(name);
        detailsMapper.insert(details);
        return Result.success();
    }
    private Details getDetailsByMd5(String md5) {
        QueryWrapper<Details> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("md5",md5);
        List<Details> detailsList=detailsMapper.selectList(queryWrapper);
        return detailsList.size() == 0 ? null : detailsList.get(0);

    }

    //通过剧名查找视频详情，详情页
    @GetMapping("/apiOne/details/finAll")
    public Result finAll(String name){
            QueryWrapper<Details> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("name",name);
            Details details = detailsMapper.selectOne(queryWrapper);
            return Result.success(details);

    }

    @GetMapping("/apiOne/details/finAllName")
    public Result finAllName(@RequestParam String name){
        List<Details> detailsList=detailsService.finAllName(name);
        return Result.success(detailsList);
    }


    @Scheduled(cron = "0 15 10 ? * 6L")
//    @Scheduled(cron = "*/6 * * * * ?")
    public  void Scheduled(){
        List<String> classity=new ArrayList<>();
        classity.add("时空之旅");
        classity.add("逆袭");
        classity.add("甜宠");
        classity.add("重生");
        for (String classifys: classity){
            String CoverList="coverList:"+classifys;
            if (CoverList!=null){
                redisTemplate.delete(CoverList);
            }
        }
      redisTemplate.delete(MaterialList);
        System.out.println("清除缓存更新数据-线程1");
    }

    //主页的推荐视频和详情页娱乐新闻的热门视频
    @GetMapping("/apiOne/details/selectTop10")
    public Result selectTop10(@RequestParam String classify){
        CoverList="coverList:"+classify;
        Object List = redisTemplate.opsForValue().get(CoverList);
        if (List == null){
            QueryWrapper<Details> queryWrapper=new QueryWrapper<>();
            queryWrapper.like("classify", classify);
            queryWrapper.last("limit 12"); // Limit to top 10
            List<Details> details= detailsMapper.selectList(queryWrapper);
            if (details!=null){
                redisTemplate.opsForValue().set(CoverList,details,2,TimeUnit.HOURS);
                return Result.success(details);
            }
        }
        return Result.success(List);
//        List<Details> details= detailsMapper.selectList(queryWrapper);
//        return Result.success(details);
    }

    @GetMapping("/apiOne/details/selectTop12")
    public Result selectTop12() {

        QueryWrapper<Details> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", "电影");
        queryWrapper.last("ORDER BY RAND() LIMIT 12"); // 随机获取12条记录

        List<Details> details = detailsMapper.selectList(queryWrapper);
        return Result.success(details);
    }
    @GetMapping("/apiOne/details/selectTeleplay")
    public Result selectTeleplay() {

        QueryWrapper<Details> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", "电视剧");
        queryWrapper.last("ORDER BY RAND() LIMIT 12"); // 随机获取12条记录

        List<Details> details = detailsMapper.selectList(queryWrapper);
        return Result.success(details);
    }
    @GetMapping("/apiOne/details/selectVariety")
    public Result selectVariety() {

        QueryWrapper<Details> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", "综艺");
        queryWrapper.last("ORDER BY RAND() LIMIT 12"); // 随机获取12条记录

        List<Details> details = detailsMapper.selectList(queryWrapper);
        return Result.success(details);
    }
    @GetMapping("/apiOne/details/selectAnimation")
    public Result selectAnimation() {

        QueryWrapper<Details> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", "动漫");
        queryWrapper.last("ORDER BY RAND() LIMIT 12"); // 随机获取12条记录

        List<Details> details = detailsMapper.selectList(queryWrapper);
        return Result.success(details);
    }

    //个人素材episodes
    @GetMapping("/apiOne/details/personalMaterial")
    public Result personalMaterial(@RequestParam String username){

        MaterialList="personalMaterial:"+username;
        Object List = redisTemplate.opsForValue().get(MaterialList);
        if (List == null){
            QueryWrapper<Details> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("username", username);
            List<Details> details= detailsMapper.selectList(queryWrapper);
            if (details!=null){
                redisTemplate.opsForValue().set(MaterialList,details,2, TimeUnit.HOURS);
                return Result.success(details);
            }
        }
        return Result.success(List);
    }

    //查看详情
    @GetMapping("/api/episodes/list/{id}")
    public Result getEpisodes(@PathVariable String id){
        QueryWrapper<FileOne> fileOneQueryWrapper = new QueryWrapper<>();
        fileOneQueryWrapper.eq("details_id",id);
        List<FileOne> fileOnes = fileMapper.selectList(fileOneQueryWrapper);
        return Result.success(fileOnes);
    }

    //首页的大视频
    @GetMapping("/apiOne/details/selectTopSix")
    public Result selectTopSix(@RequestParam String classify){
        String selectTopSix="selectTopSix:"+classify;
        Object selectTopList = redisTemplate.opsForValue().get(selectTopSix);
        if (selectTopList == null){
            QueryWrapper<Details> queryWrapper=new QueryWrapper<>();
            queryWrapper.like("classify", classify);
            queryWrapper.eq("status",1);
            queryWrapper.last("limit 6"); // Limit to top 8
            List<Details> details= detailsMapper.selectList(queryWrapper);
            if (details !=null){
                redisTemplate.opsForValue().set(selectTopSix,details,2,TimeUnit.DAYS);
                return Result.success(details);
            }
        }
        return Result.success(selectTopList);
    }

    //搜索页面默认的剧
    @GetMapping("/apiOne/details/selectAcquiesce")
    public Result selectAcquiesce(@RequestParam String classify){
        List<Details> details= detailsMapper.selectAcquiesce(classify);
        return Result.success(details);

    }
    //点赞增加
    @GetMapping("/apiOne/details/likes")
    public Result likes(@RequestParam String name){
        Details details = detailsService.selectLikes(name);
        return Result.success(details.getQuantity());
    }

    //点赞删除
    @GetMapping("/apiOne/details/likesDelete")
    public Result collectDelete(@RequestParam String name){
        return Result.success(detailsService.selectlikesDelete(name).getQuantity());
    }

    //收藏增加
    @GetMapping("/apiOne/details/collect")
    public Result Collect(@RequestParam String name){
        return Result.success(detailsService.selectCollect(name).getCollect());
    }

    //收藏取消
    @GetMapping("/apiOne/details/collectDelete")
    public Result CollectDelete(@RequestParam String name){
        return Result.success(detailsService.selectCollectDelete(name).getCollect());
    }

    //分页组件，搜索短剧
    @GetMapping("/apiOne/details/page")
    public Result findPage(@RequestParam(defaultValue = "") String name,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize){
        QueryWrapper<Details> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("details_id");
        if (!"".equals(name)) {
            queryWrapper.like("name", name);
        }
        Page<Details> page = detailsService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }

    //模糊查询
    @GetMapping("/api/details/paging")
    public Result findPaging(@RequestParam(defaultValue = "") String name,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                             @RequestParam String username,
                             @RequestParam String type) {
        //QueryWrapper 来构建查询条件，并基于条件执行了分页查询
        QueryWrapper<Details> queryWrapper = new QueryWrapper<>();

        if (!"".equals(type)){
            queryWrapper.eq("type",type);
        }
        queryWrapper.orderByDesc("details_id");
        queryWrapper.isNotNull("username");
        // 如果 name 或 classify 中有一个不为空，则添加对应的查询条件
        if (!"".equals(name)) {
            queryWrapper.like("name",name);
        }
        if (!"".equals(username)){
            queryWrapper.like("username",username);
        }

        Page<Details> page = detailsService.page(new Page<>(pageNum, pageSize), queryWrapper);

        return Result.success(page);
    }

    @GetMapping("/api/details/pagingTop3")
    public Result findPagingTop3() {

        List<Details> page = detailsService.findPagingTop3();

        return Result.success(page);
    }

    //模糊查询
    @GetMapping("/api/details/pagingList")
    public Result findPagingList(@RequestParam(defaultValue = "") String name,
                                 @RequestParam Integer pageNum,
                                 @RequestParam Integer pageSize,
                                 @RequestParam String type
    ) {
        //QueryWrapper 来构建查询条件，并基于条件执行了分页查询
        QueryWrapper<Details> queryWrapper = new QueryWrapper<>();
        System.out.println("type===================="+type);
        if (!"".equals(type)){
            queryWrapper.eq("type",type);
        }
        queryWrapper.orderByDesc("details_id");
        // 如果 name 或 classify 中有一个不为空，则添加对应的查询条件
        if (!"".equals(name)) {
            queryWrapper.like("name",name);
        }

        Page<Details> page = detailsService.page(new Page<>(pageNum, pageSize), queryWrapper);

        return Result.success(page);
    }

    @GetMapping("/api/details/MypagingList")
    public Result MyFindPagingList(@RequestParam(defaultValue = "") String name,
                                 @RequestParam String username,
                                 @RequestParam Integer pageNum,
                                 @RequestParam Integer pageSize) {
        //QueryWrapper 来构建查询条件，并基于条件执行了分页查询
        QueryWrapper<Details> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("username",username).orderByDesc("details_id");
        // 如果 name 或 classify 中有一个不为空，则添加对应的查询条件
        if (!"".equals(name)) {
            queryWrapper.like("name",name);
        }

        Page<Details> page = detailsService.page(new Page<>(pageNum, pageSize), queryWrapper);

        return Result.success(page);
    }
    @DeleteMapping("/api/details/{id}")
    public Result deleteById(@PathVariable Integer id){
        detailsService.removeById(id);
        return Result.success();
    }
    //批量删除
    @PostMapping("/api/details/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        detailsService.removeByIds(ids);
        return Result.success();
    }

    //审核通过
    @GetMapping("/api/details/examine/{id}")
    public Result examineList(@PathVariable Integer id){
        detailsMapper.updateStatus(id);
        return Result.success();
    }

    //审核不通过
    @GetMapping("/api/details/noExamine/{id}")
    public Result noExamine(@PathVariable Integer id){
        detailsMapper.updateNoExamine(id);
        return Result.success();
    }

    //视频总数
    @GetMapping("/api/details/VideoListCount")
    public Result VideoListCount(){
        QueryWrapper<Details> objectQueryWrapper = new QueryWrapper<>();
        return Result.success(detailsMapper.selectCount(objectQueryWrapper));
    }
    //收藏数量
    @GetMapping("/api/details/favoriteCountListCount")
    public Result favoriteCountListCount(){
        return Result.success(detailsMapper.selectTotalCollectCount());
    }

    /**
     * 浏览量
     * @return
     */
    @GetMapping("/api/details/viewCountListCount")
    public Result viewCountListCount(){
        return Result.success(detailsMapper.selectTotalViewCountListCount());
    }


    //视频总数
    @PostMapping("/api/details/autherVideoCount/{username}")
    public Result autherVideoCount(@PathVariable String username){
        QueryWrapper<Details> detailsQueryWrapper = new QueryWrapper<>();
        detailsQueryWrapper.eq("username",username);
        System.out.println("=============="+detailsMapper.selectCount(detailsQueryWrapper));
        return Result.success(detailsMapper.selectCount(detailsQueryWrapper));
    }
    //作者总播放量
    @PostMapping("/api/details/autherViewCountList/{username}")
    public Result autherViewCountList(@PathVariable String username){
        int count = detailsMapper.selectAutherTotalViewCountListCount(username);
        System.out.println("==" + count);
        return Result.success(count);
    }

    //作者收藏
    @PostMapping("/api/details/autherFavoriteCountList/{username}")
    public Result autherFavoriteCountList(@PathVariable String username){
        return Result.success(detailsMapper.selectAutherTotalCollectCount(username));
    }


    @GetMapping("/apiOne/details/moviePage")
    public Result page(
            @RequestParam int pageNum,
            @RequestParam int pageSize,
            @RequestParam(required = false) String type,
            @RequestParam(required = false) String genre,
            @RequestParam(required = false) String country,
            @RequestParam(required = false) String year,
            @RequestParam(required = false) String category) {

        QueryWrapper<Details> movieQueryWrapper = new QueryWrapper<>();
        movieQueryWrapper.orderByDesc("details_id");
        movieQueryWrapper.eq("type",category);

        // 处理类型
        if (StringUtils.isNotBlank(type) && !"全部".equals(type)) {
            String result = type.replace("片", ""); // 去掉“片”
            movieQueryWrapper.like("classify", result); // 使用模糊查询
        }

        // 处理年份
        if (StringUtils.isNotBlank(year) && !"全部".equals(year)) {
            movieQueryWrapper.like("year", year);
        }

        // 处理国家
        if (StringUtils.isNotBlank(country) && !"全部".equals(country)) {
            movieQueryWrapper.like("country", country);
        }

        // 处理剧情类型
        if (StringUtils.isNotBlank(genre) && !"全部".equals(genre)) {
            movieQueryWrapper.like("genre", genre); // 确认 genre 字段是否正确
        }
        // 分页查询
        Page<Details> page = detailsService.page(new Page<>(pageNum, pageSize), movieQueryWrapper);
        return Result.success(page);
    }
    //电视剧
    @GetMapping("/apiOne/details/teleplayPage")
    public Result teleplayPage(
            @RequestParam int pageNum,
            @RequestParam int pageSize,
            @RequestParam(required = false) String type,
            @RequestParam(required = false) String genre,
            @RequestParam(required = false) String country,
            @RequestParam(required = false) String year,
            @RequestParam(required = false) String category) {

        QueryWrapper<Details> movieQueryWrapper = new QueryWrapper<>();
        movieQueryWrapper.orderByDesc("details_id");
        movieQueryWrapper.eq("type",category);

        // 处理类型
        if (StringUtils.isNotBlank(type) && !"全部".equals(type)) {
            String result = type.replace("剧", ""); // 去掉“片”
            movieQueryWrapper.like("classify", result); // 使用模糊查询
        }

        // 处理年份
        if (StringUtils.isNotBlank(year) && !"全部".equals(year)) {
            movieQueryWrapper.like("year", year);
        }

        // 处理国家
        if (StringUtils.isNotBlank(country) && !"全部".equals(country)) {
            movieQueryWrapper.like("country", country);
        }

        // 处理剧情类型
        if (StringUtils.isNotBlank(genre) && !"全部".equals(genre)) {
            movieQueryWrapper.like("genre", genre); // 确认 genre 字段是否正确
        }
        // 分页查询
        Page<Details> page = detailsService.page(new Page<>(pageNum, pageSize), movieQueryWrapper);
        return Result.success(page);
    }

    //动漫
    @GetMapping("/apiOne/details/animationPage")
    public Result animationPage(
            @RequestParam int pageNum,
            @RequestParam int pageSize,
            @RequestParam(required = false) String type,
            @RequestParam(required = false) String genre,
            @RequestParam(required = false) String country,
            @RequestParam(required = false) String year,
            @RequestParam(required = false) String category) {

        QueryWrapper<Details> movieQueryWrapper = new QueryWrapper<>();
        movieQueryWrapper.orderByDesc("details_id");
        movieQueryWrapper.eq("type",category);

        // 处理类型
        if (StringUtils.isNotBlank(type) && !"全部".equals(type)) {
//            String result = type.replace("动漫", ""); // 去掉“片”
            movieQueryWrapper.like("classify", type); // 使用模糊查询
        }

        // 处理年份
        if (StringUtils.isNotBlank(year) && !"全部".equals(year)) {
            movieQueryWrapper.like("year", year);
        }

        // 处理国家
        if (StringUtils.isNotBlank(country) && !"全部".equals(country)) {
            movieQueryWrapper.like("country", country);
        }

        // 处理剧情类型
        if (StringUtils.isNotBlank(genre) && !"全部".equals(genre)) {
            movieQueryWrapper.like("genre", genre); // 确认 genre 字段是否正确
        }
        // 分页查询
        Page<Details> page = detailsService.page(new Page<>(pageNum, pageSize), movieQueryWrapper);
        return Result.success(page);
    }

    //综艺
    @GetMapping("/apiOne/details/varietyPage")
    public Result varietyPage(
            @RequestParam int pageNum,
            @RequestParam int pageSize,
            @RequestParam(required = false) String type,
            @RequestParam(required = false) String genre,
            @RequestParam(required = false) String country,
            @RequestParam(required = false) String year,
            @RequestParam(required = false) String category) {

        QueryWrapper<Details> movieQueryWrapper = new QueryWrapper<>();
        movieQueryWrapper.orderByDesc("details_id");
        movieQueryWrapper.eq("type",category);

        // 处理类型
        if (StringUtils.isNotBlank(type) && !"全部".equals(type)) {
            movieQueryWrapper.like("classify", type); // 使用模糊查询
        }

        // 处理年份
        if (StringUtils.isNotBlank(year) && !"全部".equals(year)) {
            movieQueryWrapper.like("year", year);
        }

        // 处理国家
        if (StringUtils.isNotBlank(country) && !"全部".equals(country)) {
            movieQueryWrapper.like("country", country);
        }

        // 处理剧情类型
        if (StringUtils.isNotBlank(genre) && !"全部".equals(genre)) {
            movieQueryWrapper.like("genre", genre); // 确认 genre 字段是否正确
        }
        // 分页查询
        Page<Details> page = detailsService.page(new Page<>(pageNum, pageSize), movieQueryWrapper);
        return Result.success(page);
    }

}
